<?php
include_once 'MY/extra_sql.php';
class TagController extends Zend_Controller_Action{

    function init(){
		if($_SESSION['user']==""){
			echo "<script>window.location='../user/login';</script>"; exit;
		}
		$this->registry = Zend_Registry::getInstance();
		$this->view = $this->registry['view'];
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$this->view->template = "tag";
		$this->view->conf = new Zend_Config(require './application/configs/config.php');

		$group_id = $_SESSION['user']['group_id'];
		$group = new Group();
		$group = $group->getgroupByid($group_id);
		$groups = explode(',',$group[0]['models']);
		if(!in_array('tag',$groups)){
			echo "<script>alert('No Permission To Enter!');history.go('-1');</script>";exit();
		}
	}

	public function indexAction(){
		$tag = new Tag();
		$tags = $tag->getAllTags('','','','');
		$this->view->total = count($tags);
		$this->view->top = $tag->getTopTags();
		$this->view->tags = $tags;
		
		echo $this->view->render('tag/index.html');
	}

	//添加标签方法
	public function addAction(){
		$filter=new Zend_Filter_StripTags();
		$tag = new Tag();
		$operation = new Operation();
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$name=$filter->filter(($this->_request->getPost('name')));
			$summary=$this->_request->getPost('summary');
			$key=$filter->filter(($this->_request->getPost('key')));
			$user_id=$_SESSION['user']['user_id'];

			$create_time = time();
			$data = array(
				'name'=>$name,
				'summary'=>$summary,
				'key_word'=>$key,
				'user_id'=>$user_id,
				'create_time'=>$create_time,
			    'user_id'=>$user_id
			);

			$tag_id = $tag->insert($data);
			if($tag_id){
				$time = time();
				$data = array(
					'op_name'=>'add',
					'model_name'=>'tag',
					'user_id'=>$_SESSION['user']['user_id'],
					'user_name'=>$_SESSION['user']['user_name'],
					'name'=>'增加标签',
					'create_time'=>$time
				);
				$query = $operation->insert($data);
				echo "<script>window.location='detail?tag_id=".$tag_id."'</script>";
			}else{
				
			}
		}
		echo $this->view->render('tag/add.html');
	}

	//修改标签方法
  	public function editAction(){
		$filter=new Zend_Filter_StripTags();
		$tag = new Tag();
		$operation = new Operation();
		$db = new mysql(constant('dbhost'),constant('dbuser'),constant('dbpass'),constant('dbname'),"UTF8");
		$tag_id=$filter->filter(($this->_request->getParam('tag_id')));

		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$name=$filter->filter(($this->_request->getPost('name')));
			$summary=$this->_request->getPost('summary');
			$key=$filter->filter(($this->_request->getPost('key')));
			$user_id=$filter->filter(($this->_request->getPost('user_id')));

			$create_time = time();
			$data = array(
				'name'=>$name,
				'summary'=>$summary,
				'key_word'=>$key,
				'user_id'=>$user_id,
				'create_time'=>$create_time,
			);

			$tag->updatetagBytag_id($data, $tag_id);
			$time = time();
			$data = array(
				'op_name'=>'edit',
				'model_name'=>'tag',
				'user_id'=>$_SESSION['user']['user_id'],
				'user_name'=>$_SESSION['user']['user_name'],
				'name'=>'修改标签',
				'create_time'=>$time
			);
			$query = $operation->insert($data);
			echo "<script>window.location='detail?tag_id=".$tag_id."'</script>";
		}else{
			$this->view->tag= $tag->gettagBytag_id($tag_id);
		}
		echo $this->view->render('tag/edit.html');
	}


	//标签列表页
	public function listAction(){
		$filter=new Zend_Filter_StripTags();
		$tag = new Tag();
        $operation = new Operation();
		$page=$filter->filter(($this->_request->getParam('page')));
		$search_name=$filter->filter(($this->_request->getParam('search_name')));
		$search_key=$filter->filter(($this->_request->getParam('search_key')));
		if($page==""){
			$page="1";
		}
		$count = $tag->getAllTags('','', $search_name,$search_key);
//总条数
		$total = count($count);
//总页数
		$perpage = "10";
		$pages = ceil($total/$perpage);

		$this->view->tags = $tag->getAllTags($page, $perpage, $search_name,$search_key);

		$this->view->page = $page;
		$this->view->pages = $pages;
		$this->view->total = $total;
		$time = time();
		$data = array(
			'op_name'=>'list',
			'model_name'=>'tag',
			'user_id'=>$_SESSION['user']['user_id'],
			'user_name'=>$_SESSION['user']['user_name'],
			'name'=>'取得所有标签',
			'create_time'=>$time
		);
		$query = $operation->insert($data);
		echo $this->view->render('tag/list.html');
	}

	//标签详情页
	public function detailAction(){
		$filter=new Zend_Filter_StripTags();
		$tag = new Tag();
		$operation = new Operation();
		$db = new mysql(constant('dbhost'),constant('dbuser'),constant('dbpass'),constant('dbname'),"UTF8");
		$tag_id=$filter->filter(($this->_request->getParam('tag_id')));
		$page=$filter->filter(($this->_request->getParam('page')));
		$stu_page=$filter->filter(($this->_request->getParam('stu_page')));
		$pro_page=$filter->filter(($this->_request->getParam('pro_page')));
		$new_tag = $tag->gettagBytag_id($tag_id);
		$this->view->tag = $new_tag;
		$key_word = $new_tag[0]['key_word'];
	    if ($stu_page=="") {
			$stu_page="1";
		}
        if ($pro_page=="") {
			$pro_page="1";
		}
		
		//标签相关项目
		$project = new Project();
		$projecttag = new ProjectTag();
		$arrProjectTags = $project->getProjectRelated(array($key_word),9999,'');
		
		//总条数
		$pro_total = count($arrProjectTags);

		//总页数
		$perpage = "10";
		$pro_pages = ceil($pro_total/$perpage);
		$pro_start = ($pro_page-1)*$perpage;
		$pro_end = $perpage;
		$this->view->pro_page = $pro_page;
		$this->view->pro_pages = $pro_pages;
		$this->view->pro_total = $pro_total;
	
		$arrProjectTags = $project->getProjectRelated(array($key_word),$pro_page,$pro_end);

        $arrProjects = array();
		foreach ($arrProjectTags as $row) {
			$arrProject = $project->getprojectByproject_id($row['project_id']);
			$arrProjects[] = $arrProject[0];
		}
		$this->view->projects = $arrProjects;
		
		//标签相关学生
		$student = new Student();
		$studenttag = new StudentTag();
		
		$arrStudentTags = $studenttag->getstudenttagBytag_id($tag_id,9999,'');
		
		//总条数
		$stu_total = count($arrStudentTags);

		//总页数
		$perpage = "2";
		$stu_pages = ceil($stu_total/$perpage);
		$stu_start = ($stu_page-1)*$perpage;
		$stu_end = $perpage;
		$this->view->stu_page = $stu_page;
		$this->view->stu_pages = $stu_pages;
		$this->view->stu_total = $stu_total;
		$arrStudents = array();
		$arrStudentTags = $studenttag->getstudenttagBytag_id($tag_id,$stu_start,$stu_end);
		foreach ($arrStudentTags as $row) {
			$arrStudent = $student->getstudentBystudent_id($row['student_id']);
			$arrStudents[] = $arrStudent[0];
		}
		$this->view->students = $arrStudents;
		
		if($page=="") $page="1";
		$count = $db->select_comment('','',$tag_id,'7');
		$total = count($count);
		$perpage = "2";
		$pages = ceil($total/$perpage);
		$start = ($page-1)*$perpage;
		$end = $perpage;
		$this->view->page = $page;
		$this->view->pages = $pages;
		$this->view->total = $total;

		$this->view->comments = $db->select_comment($start,$end,$tag_id,'7');
		$time = time();
		$data = array(
			'op_name'=>'detail',
			'model_name'=>'tag',
			'user_id'=>$_SESSION['user']['user_id'],
			'user_name'=>$_SESSION['user']['user_name'],
			'name'=>'查看标签详情',
			'create_time'=>$time
		);
		$query = $operation->insert($data);
		echo $this->view->render('tag/detail.html');
	}

	//删除标签
    public function deleteAction(){
		$filter=new Zend_Filter_StripTags();
		$tag = new Tag();
        $operation = new Operation();
		$tag_id=$filter->filter(($this->_request->getParam('tag_id')));
		$query = $tag->deltagBytag_id($tag_id);

		if($query){
			$time = time();
			$data = array(
				'op_name'=>'delete',
				'model_name'=>'tag',
				'user_id'=>$_SESSION['user']['user_id'],
				'user_name'=>$_SESSION['user']['user_name'],
				'name'=>'删除标签',
				'create_time'=>$time
			);
			$query = $operation->insert($data);
			echo "<script>window.location='list'</script>";
		}else{
			 
		}
	}

	public function jsonAction(){
		$filter=new Zend_Filter_StripTags();
		$tag = new Tag();
		$tag_id = $filter->filter(($this->_request->getParam('tag_id')));
		if (empty($tag_id)) $tag_id = 1;
		$arrTags[1] = array('飞机','制造','卫星');
		$arrTags[2] = array('电影','培训','电脑');
		$arrTags[3] = array('房地产','出租屋','网吧');
		$strTags = json_encode($arrTags[$tag_id]);
		$this->view->tags = $strTags;
		//$this->view->tags = $arrTags[$tag_id];
		echo $this->view->render('tag/json.html');
	}
}
?>
